package 代码记录.校招笔试真题.笔试真题_2022.游卡;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zx
 * @create 2022-09-20 19:22
 */
public class Test2 {
    /**
     * @return
     * "deeedbbcccbdaa",3
     * "aa"
     */
    public static void main(String[] args) {
        String s = "deeedbbcccbdaa";
        int k = 3;
        removeDuplicates(s,k);
    }
    public static String removeDuplicates (String s, int k) {
        while(s.length() >= k){
            for(int left = 0,right = 0;right < s.length();right++){
                if(right - left >= 2 && s.charAt(left) == s.charAt(right)){
                    while (s.charAt(right) == s.charAt(left)){
                        right++;
                    }
                    s = s.substring(0,left) + s.substring(right,s.length());
                    if(s.length() < k){
                        return s;
                    }
                    break;
                }
                if(s.charAt(left) == s.charAt(right)){
                    continue;
                }else{
                    left = right;
                }
            }
        }
        return s;
    }
}
